package stardust.product.xspider;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.StringFilter;
import org.htmlparser.util.NodeList;
import stardust.core.io.IOUtils;
import stardust.core.network.protocol.http.HttpRequestMeta;
import stardust.core.network.protocol.http.HttpResponseMeta;


public class YD_XSpiderCallback implements XSpiderCallback
{
  
  private List<String> historyUrl = new ArrayList<String>();
  
  private List<String> url;

  public synchronized XSpiderURL nextURL()
  {
    if (this.url == null)
    {
      this.url = new ArrayList<String>();
//      this.url.add("http://yunduan.org.cn/");
      
      for (int i = 0; i < 20; i++)
      {
        this.url.add("http://yunduan.org.cn/softlist/" + i + "/");
      }
    }
    
    if (this.url.isEmpty())
    {
      return null;
    }
    else
    {
      XSpiderURL u = new XSpiderURL();
      
      u.setUrl(this.url.remove(0));
      this.historyUrl.add(u.getUrl());
      
      return u;
    }
  }

  public synchronized void onError(XSpiderURL url, Throwable e)
  {
    this.url.add(url.getUrl());
    e.printStackTrace();
  }
  
  private int count;

  public void resolve(XSpiderURL url, HttpRequestMeta reqMeta, HttpResponseMeta rspMeta, String encode, String title, String html)
  {
    Parser parser;
    NodeList nl;
    
    if (html == null)
    {
      System.out.println("丢弃其它页面：" + url.getUrl() + " (" + this.url.size() + "/" + this.historyUrl.size() + "=" + this.count + ")");
      return;
    }
    
    try
    {
      parser = Parser.createParser(html, encode);
      
      if (parser.parse(new StringFilter("源码大小")).size() > 0)
      {
        this.count++;
        System.err.println("产品页面：" + url.getUrl() + " (" + this.url.size() + "/" + this.historyUrl.size() + "=" + this.count + ")");
      }
      else
      {
        System.out.println("丢弃页面：" + url.getUrl() + " (" + this.url.size() + "/" + this.historyUrl.size() + "=" + this.count + ")");
        return;
      }
      
      parser.reset();
      nl = parser.parse(new HasAttributeFilter("class", "box_title"));
      String appClass = nl.size() > 0 ? ((Tag) (nl.elementAt(0).getChildren().elementAt(5))).getFirstChild().toHtml() : "未知";
      
      parser.reset();
      nl = parser.parse(new HasAttributeFilter("class", "downtb"));
      String appImage;
      
      try
      {
        appImage = nl.size() > 0 ? ((Tag) (nl.elementAt(0).getChildren().elementAt(1).getFirstChild())).getAttribute("src") : "未知";
      }
      catch (Exception e)
      {
        appImage = nl.size() > 0 ? ((Tag) (nl.elementAt(0).getChildren().elementAt(2))).getAttribute("src") : "未知";
      }
      
      parser.reset();
      nl = parser.parse(new HasAttributeFilter("id", "tt1"));
      String appName = nl.size() > 0 ? nl.elementAt(0).getFirstChild().getText() : "未知";
      
      parser.reset();
      nl = parser.parse(new HasAttributeFilter("class", "downc"));
      String appContent = nl.size() > 0 ? nl.elementAt(0).getChildren().elementAt(1).getFirstChild().toHtml() : "未知";
      
      IOUtils.write("data/yd/" + title + ".txt", (appName + "\n" + appClass + "\n" + appImage + "\n" + appContent).getBytes(), false);
    }
    catch (Exception e)
    {
      System.out.println(url.getUrl() + " - " + e);
      e.printStackTrace();
    }
  }

  public synchronized void saveURL(XSpiderURL from, List<String> url)
  {
    for (String i : url)
    {
      if (!this.url.contains(i) && !this.historyUrl.contains(i) && i.indexOf("yunduan.org.cn") != -1)
      {
        this.url.add(i);
      }
    }
  }
  
}
